const kurento = require("kurento-client");
var mqtt = require('mqtt');
var path = require('path');
var fs = require('fs');
var format = require('string-format');
const e = require("express");
format.extend(String.prototype);

class TKmsImp {
    constructor() {
        this.config = {
            kmsUrl: "wss://wss.hhdata.cn:8433/kurento",
            tagKey: "tagKey-kms"
        }
        this.kmsClient = null;
    }
    getClient() {
        var me = this;
        return new Promise((success, faild) => {
            try {
                if (me.kmsClient && me.kmsClient != null) {
                    success(me.kmsClient);
                } else {
                    var pingSvr = () => {
                        try {

                        } catch (eer) {
                            console.log(eer);
                        }
                    }
                    kurento(me.config.kmsUrl, function(erClient, _client) {
                        me.kmsClient = _client;
                        _client.on('disconnect', function() {
                            delete me["kmsClient"];
                        });
                        _client.getServerManager().then(_svr => {
                            _client.svr = _svr;
                            _svr.on('ObjectDestroyed', function(data) {

                            });
                            me.kmsClient = _client;
                            success(_client);
                        })
                    });
                }
            } catch (er) {
                console.log(er);
                success();
            }
        });
    }

}